﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Jogo_Grupo2
{
    class Player : Objeto2D
    {
       

        float timer = 0;
        float intervalo = 70f;
        int curframe;

        int spriteWidth;
        int spriteHeight;

        int velocidade = 2;
        SpriteEffects inverted = SpriteEffects.None;

        KeyboardState lastk, currentk, teclado;

        enum heroState
        {
            parado,
            atck,
            correr,
            parado2,
        }

        heroState estadoAtual = heroState.parado;

        Rectangle spriteRect;

         public List<Tiro> ListaTiros;

        public Player(Texture2D textura, Vector2 position) : base(textura, position)
        {
            curframe = 0;

            this.texture = textura;
            spriteWidth = textura.Width / 8;
            spriteHeight = textura.Height / 1;

            spriteRect = new Rectangle(curframe * spriteWidth, curframe * spriteHeight, spriteWidth, spriteHeight);

            ListaTiros = new List<Tiro>();

        }

        public void trocarAnimacao(Texture2D textura)
        {
            curframe = 0;

            this.texture = textura;
            spriteWidth = textura.Width / 8;
            spriteHeight = textura.Height / 1;

            spriteRect = new Rectangle(curframe * spriteWidth, curframe * spriteHeight, spriteWidth, spriteHeight);
        }

      

        public override void Update(GameTime gameTime)
        {

            correr();
            verifyAnimation(gameTime);
            verifyStates();
            atirar();
         

            base.Update(gameTime);
            
        }

        public void TiroDisparado()
        {
            if (inverted == SpriteEffects.None)
            {
                Tiro tiro = new Tiro(Game1.instancia.Content.Load<Texture2D>("tiro"), new Vector2(this.position.X + 80, this.position.Y + 45), "direita");
                ListaTiros.Add(tiro);
            }

            else
            {
                Tiro tiro = new Tiro(Game1.instancia.Content.Load<Texture2D>("tiro"), new Vector2(this.position.X -1, this.position.Y + 45), "esquerda");
                ListaTiros.Add(tiro);

            }


        }

        private void verifyAnimation(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (curframe >= 7)
            {
                curframe = 0;
            }

            if (timer >= intervalo)
            {
                timer = 0;
                curframe++;
            }


            for (int i = 0; i < ListaTiros.Count(); i++)
            {
                ListaTiros[i].Update(gameTime);
            }

            
            spriteRect.X = curframe * spriteWidth;
        }

        private void verifyStates()
        {
            if (currentk.IsKeyUp(Keys.Right) && currentk.IsKeyUp(Keys.Left) && estadoAtual != heroState.parado && estadoAtual != heroState.atck)
            {
                estadoAtual = heroState.parado;
                trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("teste"));

                if (curframe >= 7)
                {
                    curframe = 0;
                    
                }
            }

            if (estadoAtual == heroState.correr)
            {
                this.position.X += velocidade;    
            }
            

            if (estadoAtual == heroState.atck)
            {
                //Game1.instancia.TiroDisparado();
                if (teclado.IsKeyUp(Keys.Space))
                {
                    if (curframe >= 7)
                    {
                        curframe = 0;
                        estadoAtual = heroState.parado;
                        trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("teste"));
                    }
                }

            }

        }
        

        public void correr()
        {
            lastk = currentk;
            currentk = Keyboard.GetState();
           

            if (currentk.IsKeyDown(Keys.Right))
                {
                    velocidade = 2;
                    inverted = SpriteEffects.None;

                    if (estadoAtual != heroState.correr && estadoAtual != heroState.atck)
                    {
                        
                        estadoAtual = heroState.correr;
                        trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("correr"));
                    }
                    
                }

            if (currentk.IsKeyDown(Keys.Left))
                {

                    velocidade = -2;
                    inverted = SpriteEffects.FlipHorizontally;

                    if (estadoAtual != heroState.correr && estadoAtual != heroState.atck)
                    {
                        
                        estadoAtual = heroState.correr;
                        trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("correr"));
                    }
                }
            
        }


       public void atirar()
       {
           if (currentk.IsKeyDown(Keys.Space) && lastk != currentk)   
            {
                
                if (estadoAtual != heroState.atck)
                    {
                            estadoAtual = heroState.atck;
                            trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("atck"));
                            TiroDisparado();
                            
                    }
            }

        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, new Rectangle((int)position.X,(int)position.Y, spriteWidth, spriteHeight), spriteRect, Color.White,0,Vector2.Zero,inverted,0);
            for (int i = 0; i < ListaTiros.Count(); i++)
            {
                ListaTiros[i].Draw(spriteBatch);
            }
          
        }

    }
}
